/*
 * @(#)CacheEntryProvider.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.service.supplement;

import com.nhncorp.usf.core.cache.CacheKey;
import com.nhncorp.usf.core.cache.CacheManager;
import com.nhncorp.usf.core.cache.CacheStatusException;
import com.nhncorp.usf.core.cache.ResultCacheKey;
import com.nhncorp.usf.core.config.runtime.Method;
import com.nhncorp.usf.core.service.ExecutionException;
import com.nhncorp.usf.core.service.ServiceExecutionContext;

/**
 * @author Web Platform Development Team.
 */
public class CacheEntryProvider implements PreSupplement {

    /**
     * @param context ServiceExecutionContext
     * @return boolean
     * @throws ExecutionException the ExecutionException
     * @see com.nhncorp.usf.core.service.supplement.PreSupplement#beforeExecute(com.nhncorp.usf.core.service.ServiceExecutionContext)
     */
    public boolean beforeExecute(ServiceExecutionContext context) throws ExecutionException {
        Method method = context.getMethod();
        CacheKey key = new ResultCacheKey(context.getBo().getId(), method.getName(), context.getMethodParameter());
        int expire = method.getExpire();
        try {
            Object cachedObject = CacheManager.getInstance().getCache(key, expire);

            if (cachedObject == null) {
                context.setEnableAddCache(true);
                return true;
            }

            context.setAttribute(method.getReturnId(), cachedObject);
            context.setEnableAddCache(false);
        } catch (CacheStatusException e) {
            throw new ExecutionException(e);
        }

        // chchedObject가 존재할 경우  Method Call을 수행하지 않기 위해 false를 리턴
        return false;
    }
}
